//
//  HomeNewViewController.swift
//  UserApp_iOS
//
//  Created by BestVast on 2021/5/27.
//  Copyright © 2021 linlin. All rights reserved.
//

import UIKit
/// 新的首页
class HomeNewViewController: BaseViewController {
    var scrollView: UIScrollView!
    // 第一部分
    var firstView: HomeFirstView!
//    第二部分
    var secondView: HomeEnquiryView!
//    第三部分
    var thirdView: HomeThirdView!
//    第四部分
    var fourthView: HomeShopView!
//    第五部分的轮播图
    var swiperView: SwiperNewView!
//    第六部分 推荐商家
    var recommandShopView: HomeRecommendShopView!
//    第七部分 热卖精选
    var bestSellerView: HomeBestSellerView!
    // 推荐商家数组
    var shops: [String: String] = ["title": "1"]
    // 热卖商品数组
    var sellers: [String: String] = ["title": "1"]
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.uiConfig();
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) { [self] in
            self.shops = ["title1": "1", "title2": "1", "title3": "1", "title4": "1", "title5": "1", "title6": "1"]
            self.sellers = ["title1": "1", "title2": "1", "title3": "1", "title4": "1", "title5": "1", "title6": "1", "title7": "1", "title8": "1", "title9": "1", "title10": "1", "title11": "1"]
            self.recommandShopView.shops = self.shops
            self.bestSellerView.sellers = self.sellers
            self.refreshScrollHeight()
        }
    }
    
    func uiConfig() -> Void {
        self.view.backgroundColor = .white
        self.configBottomView()
        self.configScrollView()
    }
    func configBottomView() -> Void {
        let topView = UIView.init(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 300))
        topView.backgroundColor = colorTheme
        self.view.addSubview(topView)
        
        let bottomView = UIView.init(frame: CGRect(x: 0, y: topView.bottom, width: screenWidth, height: screenHeight - 300))
        bottomView.backgroundColor = UIColor(red: 239/255, green: 239/255, blue: 239/255, alpha: 1)
        self.view.addSubview(bottomView)
    }
    
    func configScrollView() -> Void {
        scrollView = UIScrollView.init(frame: self.view.bounds)
        self.view.addSubview(scrollView)
        scrollView.backgroundColor = .clear
        scrollView.contentSize = CGSize.init(width: screenWidth, height: 1100)
        
        firstView = HomeFirstView.init(frame: CGRect.init(x: 10, y: 10, width: screenWidth - 20, height: 130))
        scrollView.addSubview(firstView)
        
        secondView = HomeEnquiryView.init(frame: CGRect.init(x: 0, y: firstView.bottom + 20, width: screenWidth, height: 130))
        secondView.callback = ({ (index) in
            debugPrint(index)
        })
        scrollView.addSubview(secondView)
        
        thirdView = HomeThirdView.init(frame: CGRect.init(x: 10, y: secondView.bottom + 20, width: screenWidth - 20, height: 300))
        scrollView.addSubview(thirdView)
        
        fourthView = HomeShopView.init(frame: CGRect.init(x: 0, y: thirdView.bottom + 20, width: screenWidth, height: 130))
        fourthView.callback = ({ (index) in
            debugPrint(index)
            let vc = MomentsVC()
            vc.modalPresentationStyle = .fullScreen

            self.present(vc, animated: false) {
                
            }
//            present(vc, animated: true)
        })
        scrollView.addSubview(fourthView)


        let five = UIView.init(frame: CGRect.init(x: 10, y: fourthView.bottom + 20, width: screenWidth - 20, height: 100))
        swiperView = SwiperNewView.init(frame: five.bounds)
        five.addSubview(swiperView)
        let navigate1 = AppNavigate.init();
        navigate1.LoctionUrl = "http://qipei128.com/AppShop/SupplierDetails.aspx?supplierId=19888"
        let navigate2 = AppNavigate.init();
        navigate2.ImageUrl = "http://www.qipei128.com/Storage/master/navigate/20190906125315_2998.jpg"
        let navigate3 = AppNavigate.init();
        navigate3.ImageUrl = "http://www.qipei128.com/Storage/master/navigate/20200909151351_7639.jpg"
        let navigate4 = AppNavigate.init();
        navigate4.LoctionUrl = "http://qipei128.com/AppShop/SupplierDetails.aspx?supplierId=37820"
        let navigate5 = AppNavigate.init();
        navigate5.ImageUrl = "http://www.qipei128.com/Storage/master/navigate/20201213172046_5121.jpg"
        let navigate6 = AppNavigate.init();
        navigate6.ImageUrl = "http://www.qipei128.com/Storage/master/navigate/20201213180326_7621.jpg"
        let navigates = [navigate1, navigate2, navigate3, navigate4, navigate5, navigate6]
        swiperView.navigats = navigates
        
        five.backgroundColor = .white
        five.layer.cornerRadius = 10
        five.layer.masksToBounds = true
        scrollView.addSubview(five)
        
        // 前五部分高度一共880，后面两部分默认80的高度，加上间隔一共1080的高度
        // 推荐商家和热卖精选 标题部分占80
        recommandShopView = HomeRecommendShopView.init(frame: CGRect.init(x: 10, y: five.bottom + 20, width: screenWidth - 20, height: 90))
        scrollView.addSubview(recommandShopView)
        
        bestSellerView = HomeBestSellerView.init(frame: CGRect.init(x: 10, y: recommandShopView.bottom + 20, width: screenWidth - 20, height: 90))
        scrollView.addSubview(bestSellerView)
    }
    func refreshScrollHeight() -> Void {
        // 前五部分高度一共880，后面两部分默认80+10的高度，加上间隔一共1080的高度
        // 推荐商家和热卖精选 标题部分占80
        // 推荐商家cell高度为 60
        // 热卖精选cell高度为 (screenWidth - 50) / 2 + 50
        if shops.count > 0 || sellers.count > 0 {
            var recoLineNum = shops.count / 2
            if shops.count % 2 > 0 {
                recoLineNum += 1
            }
            var recoHeight: CGFloat = CGFloat(recoLineNum * 60);
            if recoLineNum > 1  {
                recoHeight += CGFloat((recoLineNum - 1) * 10)
            }
            
            var sellLineNum = sellers.count / 2
            if sellers.count % 2 > 0 {
                sellLineNum += 1
            }
            var sellHeight: CGFloat = CGFloat(sellLineNum * ((Int(screenWidth) - 50) / 2 + 50))
            if sellLineNum > 1 {
                sellHeight += CGFloat((sellLineNum - 1) * 10)
            }
            recommandShopView.height = recoHeight + 90
            bestSellerView.top = recommandShopView.bottom + 20
            bestSellerView.height = sellHeight + 90
            scrollView.contentSize = CGSize(width: screenWidth, height: 1100 + recoHeight + sellHeight)
        }
        
    }
}
